package com.algorithm.hqy;

/**
 * @Classname ArrayQueue
 * @Date 2019/9/16 14:11
 * @auther by hqy
 * 循环队列
 */
public class CircleArrayQueue {

    private int maxSize;

    private int front;

    private int rear;

    private int array[];

    public CircleArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        array = new int[maxSize];
    }

    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    public boolean isEmpty() {
        return rear == front;
    }

    /**
     * @param [num]
     * @return void
     * @Description
     */
    public void addNum(int num) {
        if (isFull()) {
            throw new RuntimeException("队列已满，不能添加");
        }


        array[rear] = num;

        rear = (rear + 1) % maxSize;
    }

    /**
     * @param []
     * @return int
     * @Description 这种数组模拟队列的方法，取完这个数后，这个数实际上还是存在于这个队列中的。
     */
    public int getNum() {
        if (isEmpty()) {
            throw new RuntimeException("队列已空，不能取出数据");
        }

        int temp = array[front];
        front = (front + 1) % maxSize;
        return temp;
    }

    /**
     * @param []
     * @return void
     * @Description 展示出队列中的现有数据
     */
    public void showQuere() {
        if (isEmpty()) {
            throw new RuntimeException("队列已空~~~~~");
        }

        int queueSize = (rear - front + maxSize) % maxSize;
        for (int i = front; i < front + queueSize; i++) {
            System.out.printf("array[%d]=%d \n", i % (maxSize - 1), array[i % maxSize]);
        }
    }

}

